package dao;
import java.util.List;

import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import util.HibernateUtil;
import entity.ItemVenda;
import entity.ItemParcela;
import entity.TipoProduto;

public class ItemVendaDaoImplementation implements ItemVendaDao {

	public void save(ItemVenda itemVenda) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.save(itemVenda);
		transaction.commit();
		session.close();
	}

	public void update(ItemVenda itemVenda) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.update(itemVenda);
		transaction.commit();
	}

	public void delete(ItemVenda itemVenda) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.delete(itemVenda);
		transaction.commit();
	}

	public void consult(ItemVenda itemVenda) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		itemVenda = (ItemVenda) session.load(ItemVenda.class, new String("ItemVenda"));
	}

	public ItemVenda getItemVenda(long id) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		return (ItemVenda) session.load(ItemVenda.class, id);
	}
	
	public List<ItemVenda> listVenda() 
	{
	Session session = HibernateUtil.getSessionFactory().openSession();
	Transaction transaction = session.beginTransaction();
	String sql = "SELECT new ItemVenda( i.idItemVenda, i.quantidade, i.produto) FROM ItemVenda i";
	List<ItemVenda> lista = session.createQuery(sql).list();
	transaction.commit();
	session.close();
	return lista;
	}
	
	@Override
	public List<ItemVenda> list() {
		Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        String sql = "FROM ItemVenda WHERE venda="+null;
        List<ItemVenda> listaVenda = session.createQuery(sql).list();
        transaction.commit();
        session.close();
        return listaVenda;
	}
	

}
